<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      :close-on-click-modal="false"
      title="添加站内信"
      :modal-append-to-body="false"
      :visible.sync="visible"
      :show="show"
      :before-close="closeDialog"
      @close="$emit('update:show',false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="100px">
        <el-form-item label="收件人" prop="username">
          <el-col :span="4">
            <el-input placeholder v-model="data.username" style="width:220px" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <el-col :span="4">
            <el-input type="textarea" v-model="data.content" style="width:400px;" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="链接" prop="links">
          <el-col :span="4">
            <el-input placeholder v-model="data.links" style="width:400px" clearable></el-input>
          </el-col>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="addUpdate('data')"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped></style>

<script>
export default {
  data() {
    var checkTitle = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("标题不能为空"));
      } else {
        callback();
      }
    };

    return {
      value: "",
      data: {
        id: "",
        uid: "",
        username: "",
        content: "",
        links: ""
      },
      loading: false,
      dataYZ: {
        title: [{ validator: checkTitle, trigger: "blur" }]
      },
      visible: this.show
    };
  },
  props: {
    show: Boolean,
    editId: String,
    clickNum: Number
  },
  methods: {
    init() {
      this.data.username = "";
      this.data.content = "";
      this.data.links = "";
    },
    closeDialog() {
      //this.$refs['data'].resetFields();
      this.visible = false;
    },
    addUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["username"] = this.data.username;
          params["content"] = this.data.content;
          params["links"] = this.data.links;
          this.$api.getUser.letterSaveAdd(params).then(res => {
            console.log(res);
            if (res.data.code == 200 && res.data.result != null) {
              this.init();
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "添加成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    show() {
      console.log(this.show);
      this.visible = this.show;
    },
    clickNum(id) {
      console.log("点击button次数", id);
      console.log("编辑ID=" + this.editId);
      this.getDetails(this.editId);
    }
  },
  mounted() {},
  created() {}
};
</script>
